<!-- 登录页 -->
<template>
    <div class="full-screen">
        <c-waves>
            <div id="login-page" @keyup.enter="login" slot="center">
                <div class="login-form">
                    <div class="input-group">
                        <div class="title">欢迎登录</div>
                        <el-input :autofocus="true" placeholder="请输入用户名" icon="time" v-model="username">
                        </el-input>
                    </div>
                    <div class="input-group">
                        <el-input placeholder="请输入密码" type="password" icon="time" v-model="password">
                        </el-input>
                    </div>
                    <div class="input-group">
                        <label>记住我？</label>
                        <el-switch v-model="rememberMe" on-text="" off-text="">
                        </el-switch>
                    </div>
                    <div class="input-group">
                        <el-button @click.native="login" type="primary" :loading="isBtnLoading">{{btnText}}</el-button>
                    </div>
                </div>
            </div>
        </c-waves>
    </div>
</template>

<script>
import store from '@/store'
// import {mapState} from 'vuex'
// import { requestLogin } from '@/api/api';
import { login } from '@/api/login'
import CWaves from '@/components/CWaves'

export default {
    data() {
        return {
            username: '75727433@qq.com',
            password: '123456',
            rememberMe: false,
            isBtnLoading: false
        }
    },
    components: {
        CWaves
    },
    computed: {
        btnText() {
            if (this.isBtnLoading) return '登录中...';
            return '登录';
        },
        token() {
            return this.$store.state.token;
        }
    },
    methods: {
        login() {
            if (!this.username) {
                this.$message.error('请填写用户名');
                return;
            }
            if (!this.password) {
                this.$message.error('请填写密码');
                return;
            }
            let loginParams = { username: this.username, password: this.password };
            this.isBtnLoading = true;
            login(loginParams).then(ret => {
                console.log("Result = " + ret);
                this.isBtnLoading = false;
                let { status, message, code, data } = ret;

                if (code !== 200) {
                    this.$message.error(message);
                } else {
                    // this.$store.dispatch('token', data.token)
                    // 保存token
                    this.$store.commit('settoken', data.token);
                    // this.settoken(data.token);
                    console.log("Token = " + this.$store.state.token);
                    if (this.$route.query.redirect) {
                        this.$router.push({ path: this.$route.query.redirect });
                    } else {
                        this.$router.push({ path: '/dashboard' });
                    }
                }
            }).catch(err => {
                this.$message.error(err);
            });
        }
    }
}
</script>

<style lang="scss" scoped>
.full-screen {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

#login-page {
    // width: 100vw;
    // height: 100vh;
    // display: flex;
    justify-content: center;
    align-items: center; // background: #efeeee;
    .login-form {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 500px;
        height: 400px;
        border-radius: 10px;
        background: white;
        border: 1px #eaeaea solid;
        box-shadow: 0px 0px 25px #cac6c6;

        .title {
            color: #20a0ff;
            font-weight: bold;
            font-size: 40px;
            text-align: center;
            line-height: 2.2;
            font-family: sans-serif;
        }

        .input-group {
            margin-top: 30px;
            width: 80%;
            button {
                width: 100%;
            }
        }
    }
}
</style>
